package com.yuncheng.spcyApi.spcyService;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuncheng.spcyApi.config.SpcyMinioUtils;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcyFgbzkBasic;
import com.yuncheng.spcyApi.service.ISpcyFgbzkBasicService;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.spcyApi.vo.common.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import utils.DateConversionUtils;
import utils.Util;

import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;
import java.util.Date;

/**
 * @author hua
 */
@Service
public class FgbzkBusService {

    @Resource
    private ISpcyFgbzkBasicService spcyFgbzkBasicService;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;

    @Resource
    private BusinessConstant businessConstant;

    @Resource
    private SpcyMinioUtils spcyMinioUtils;

    @Resource
    private HnCaUtils hnCaUtils;

    /**
     * 质量体系文件盖章-根据修订类型
     */
    public void zltxwjGzByZdlx(boolean isSx, SpcyFgbzkBasic basicZltxwj){
        zltxwjToPdf(basicZltxwj, false, true, isSx);
    }

    /**
     * 生产资源库文件pdf版
     */
    public SpcyFgbzkBasic zltxwjToPdf(SpcyFgbzkBasic basicZltxwj){
        return zltxwjToPdf(basicZltxwj, true, false, false);
    }
    public SpcyFgbzkBasic zltxwjToPdf(SpcyFgbzkBasic basicZltxwj, boolean isDelete){
        return zltxwjToPdf(basicZltxwj, isDelete, false, false);
    }

    /**
     * @param isDelete  是否删除： true: 删除pdf文件
     * @param isGz 是否盖章 true: 盖章
     * @param isSx 是否盖章失效 true: 盖失效； false: 盖受控
     * @return
     */
    public SpcyFgbzkBasic zltxwjToPdf(SpcyFgbzkBasic basicZltxwj, boolean isDelete, boolean isGz, boolean isSx){
        if (isDelete) {
            if (StringUtils.isNotBlank(basicZltxwj.getFpdfurl())) {
                localFileUploadUtils.deleteLocalFileUrlByZltxwj(basicZltxwj.getFpdfurl());
            }
        }else {
            if (StringUtils.isNotBlank(basicZltxwj.getFpdfurl())){
                return basicZltxwj;
            }
        }

        if (StringUtils.isBlank(basicZltxwj.getFwjlx())){
            return basicZltxwj;
        }

        if (StringUtils.isBlank(basicZltxwj.getFwjlj())){
            return basicZltxwj;
        }

        boolean isPdf = false;
        if (basicZltxwj.getFwjlx().equals(".pdf")){
            isPdf = true;
        }else {
            // 文档才能转换
            if (!(basicZltxwj.getFwjlx().equals(".doc") || basicZltxwj.getFwjlx().equals(".docx"))) {
                return basicZltxwj;
            }
        }

        String wordUrl = ""; // 文档路径
        String parMl = ""; // pdf文件父路径
        if (businessConstant.isEnableFgbzkMinio) {
            parMl = businessConstant.TEMPORARY_UPLOAD_MKDIR_ROOT + "/" + SpcyConstant.TEMP_WJJL_CHILD_URL;
            InputStream minioInput = spcyMinioUtils.getObject(basicZltxwj.getFwjlj());
            if (minioInput == null){
                return basicZltxwj;
            }
            File file = localFileUploadUtils.asFile(minioInput, Util.getWjHz(basicZltxwj.getFwjlj()));
            wordUrl = file.getAbsolutePath();
        }else {
            wordUrl = businessConstant.UPLOAD_FILE_ROOT + basicZltxwj.getFwjlj();

            parMl = businessConstant.UPLOAD_FILE_ROOT;
        }

        /**
         * 生产pdf文件版
         */
        String ml = "/" + SpcyConstant.FGBZK_File_Base_Url + "/pdf/" + DateConversionUtils.DateToYear(new Date());
        File dir = new File(parMl + ml);
        if (!dir.exists()){
            dir.mkdirs();
        }
        String pdfUrl = ml + "/"+ GetUuIdUtils.ReplaceUuId()+".pdf";

        if (isPdf){
            boolean copyFlag = FileCopyUtils.copyFile(wordUrl, parMl+pdfUrl); // 复制文件
            if (!copyFlag){
                return basicZltxwj;
            }
        }else {
            WordPdfUtils.wordToPdf(wordUrl, parMl + pdfUrl);
        }

        // 盖章
        if (isGz){
            Result result = hnCaUtils.fileCaInFgbzkBasicInLocal(parMl + pdfUrl, isSx);
            if (!result.isSuccess()){
                return basicZltxwj;
            }
        }

        boolean flag = true;
        if (businessConstant.isEnableFgbzkMinio) {
            flag = spcyMinioUtils.putFlagFile(pdfUrl, new File(parMl + pdfUrl), Util.getWjHz(".pdf"));
        }

        if (flag) {
            basicZltxwj.setFpdfurl(pdfUrl);

            LambdaUpdateWrapper<SpcyFgbzkBasic> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyFgbzkBasic::getId, basicZltxwj.getId());

            SpcyFgbzkBasic updateBean = new SpcyFgbzkBasic();
            updateBean.setFpdfurl(pdfUrl);
            spcyFgbzkBasicService.update(updateBean, updateWrapper);
        }

        return basicZltxwj;
    }

}
